package com.tenton.controller;

import com.tenton.exception.AgeFormatException;
import com.tenton.exception.DataNullPointerException;
import com.tenton.pojo.Student;
import com.tenton.service.StudentService;
import com.tenton.utils.ConstantUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

/**
 * @Date: 2021/2/2
 * @Author: Tenton
 * @Description: 管理员操作学生数据
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    StudentService studentService;
    /**
     * 显示所有学生信息
     * @param model
     * @param pageNum
     * @return
     */
    @RequestMapping("/listStudent")
    public String listStudent(Model model,Integer pageNum) {
        studentService.findAll(pageNum,model);
        return "/listStudent";
    }

    /**
     * 跳转到增加学生界面
     * @return
     */
    @GetMapping("/toAddStudent")
    public String toAddStudent(){
        return "/addStudent";
    }

    /**
     * 增加学生
     * @param stuName
     * @param age
     * @param address
     * @param session
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/addStudent")
    @ResponseBody
    public Map<String,String> addStudent(String stuName,String idCard,int age,String address, HttpSession session) {
        //获取当前时间
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        //创建一个学生对象，保存前端传递过来的数据
        //增加学生
        try {
            return studentService.insertStudent(stuName, idCard, age, address, timestamp, session);
        } catch (AgeFormatException e) {
            e.printStackTrace();
        } catch (DataNullPointerException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 跳转到修改界面
     * @param id
     * @param model
     * @param session
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/toUpdateStudent/{id}")
    public String toUpdateStudent(@PathVariable("id") Integer id, Model model,HttpSession session){
        studentService.toUpdateStudent(id,model,session);
        //跳转到前端界面
        return "/updateStudent";
    }

    /**
     * 修改学生信息
     * @param stuId
     * @param stuName
     * @param idCard
     * @param age
     * @param address
     * @param session
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/updateStudent")
    @ResponseBody
    public Map<String,String> updateStudent(int stuId,String stuName,String idCard,int age,String address, HttpSession session) {
        try {
            return studentService.updateStudent(stuId, stuName, idCard, age, address, session);
        } catch (DataNullPointerException e) {
            e.printStackTrace();
        } catch (AgeFormatException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除学生信息
     * @param studentId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/deleteStudent")
    @ResponseBody
    public Map<String,String>  deleteStudent(Integer studentId){
        return studentService.deleteStudent(studentId);
    }
    /**
     * 跳转到查询学生信息界面
     * @return
     */
    @GetMapping("/toGetStudent")
    public String toGetStudent(){
        return "/getStudentByName";
    }

    /**
     * 模糊查询
     * @param model
     * @param name
     * @param pageNum
     * @return
     */
    @GetMapping("/getStudent")
    public String getStudent(Model model,String name,Integer pageNum){
        studentService.pageQuery(name, pageNum, model);
        //跳转到查询界面
        return "/getStudentByName";
    }

    /**
     * 跳转到查询界面
     * @return
     */
    @GetMapping("/toGetStudentByName")
    public String toGetStudentByName(){
        return "/getStudentByName";
    }
    /**
     * 跳转到批量上传界面
     * @return
     */
    @GetMapping("/toUploadExcel")
    public String toUploadExcel(){
        return "/uploadExcel";
    }

    /**
     * 批量添加学生信息
     * @param file
     * @param session
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/upload")
    @ResponseBody
    public Map<String,String> upload(@RequestParam("file") MultipartFile file, HttpSession session){
        try {
            return studentService.addStudent(file, session);
        } catch (AgeFormatException e) {
            e.printStackTrace();
        } catch (DataNullPointerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    //@Transactional(rollbackFor = Exception.class)
    //@PostMapping("/upload")
    //@ResponseBody
    //public Result upload(@RequestParam("file") MultipartFile file, HttpSession session){
    //    try {
    //        studentService.addStudent(file, session);
    //    } catch (AgeFormatException e) {
    //        e.printStackTrace();
    //        return new Result(false, MessageConstantUtil.AGE_FORMAT_FAIT);
    //    } catch (DataNullPointerException e) {
    //        e.printStackTrace();
    //        return new Result(false, MessageConstantUtil.DATA_NULL_POINT);
    //    } catch (IOException e) {
    //        e.printStackTrace();
    //        return new Result(false, MessageConstantUtil.EXCEL_NULL);
    //    }
    //    return new Result(true, MessageConstantUtil.ADD_STUDENT_SUCCESS);
    //}

    /**
     * 模板下载
     * @param fileName
     * @return
     */
    @GetMapping("/downTemplate")
    public String downTemplate(@RequestParam("fileName") String fileName){
        try {
            studentService.downTemplate(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response){
        studentService.exportData(response);
    }

    /**
     * 跳转主页
     * @param stuId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/retMain")
    @ResponseBody
    public Map<String,String> retMain(Integer stuId){
        return studentService.retMain(stuId);
    }
}
